import ToolHttpClient from "./ToolHttp";

let excludeField = ['id', 'created_by', 'created_date', 'updated_by', 'updated_date'];

export default {
	init(name, _dsId) {
		let apiAction = {
			list(data) {
				return ToolHttpClient.post(`/action/action-table-list/${name}`, {
					...data,
					_dsId
				});
			},
			page(data) {
				return ToolHttpClient.post(`/action/action-table-page/${name}`, {
					...data,
					_dsId
				});
			},
			async remove(data) {
				return await ToolHttpClient.post(`/action/action-table-remove/${name}`, {
					...data,
					_dsId
				});
			},
			async removeAlert(data) {
				return await apiAction.remove({
					...data,
					_dsId
				});
			},
			view(data) {
				return ToolHttpClient.post(`/action/action-table-view/${name}`, {
					...data,
					_dsId
				});
			},
			async meta() {
				let metas = await ToolHttpClient.post(`/action/action-table-info/${name}`, {
					_dsId
				});
				metas.forEach(item => {
					item.memo = item.memo || item.name
					if (excludeField.includes(item.name)) {
						item.hidden = true;
					}
				})
				return metas;
			},
			sync(data) {
				return ToolHttpClient.post(`/action/action-table-sync/${name}`, {
					...data,
					_dsId
				});
			},
			async dict() {
				try {
					let res = await apiAction.list();
					return res.map(item => ({
						name: item.name,
						val: item.id
					}));
				} catch (e) {
					console.error(`字典数据[${name}]异常，请联系管理员处理...`)
					return [];
				}
			}
		}
		return apiAction;
	},
	run(action, code, data) {
		return ToolHttpClient.post(`/action/${action}/${code}`, data);
	},
	enumDict(code, data) {
		return this.run("action-enum-dict", code, data);
	}
}
